<script>
import {getLobbyTypeInfo, getCinemaInfo} from "@/api";

export default {
  name: "AddLobbyView",
  data() {
    // 排序号只能是整数数字
    var validateSort = (rule, value, callback) => {
      let numSort = Number(value);
      if (value.length === 0) {
        callback(new Error('排序号不能为空'));
      }
      if (isNaN(numSort) || !(numSort === parseInt(value))) {
        callback(new Error('只能是整数数字'));
      }
      if (numSort === parseInt(value)) {
        callback();
      }
    };
    return {
      imageUrl: '',
      loading: false,
      brandValue: '',
      mallValue: '',
      mallValue2: '',
      brandValue2: '',
      lobbyTypeOptions: void 0,
      cinemaOptions: void 0,
      form: {
        lobbyTypeId: '',
        cinemaId: '',
        lobbyName: '',
        introduce: '',
        col: '',
        rowNum: '',
        pic: ''
      },
      rules: {
        lobbyName: [
          {required: true, message: '请输入影厅名', trigger: 'blur'},
        ],
        introduce: [
          {required: true, message: '请输入影厅介绍', trigger: 'blur'},
        ],
        col: [
          {required: true, message: '请输入列', trigger: 'blur'},
        ],
        rowNum: [
          {required: true, message: '请输入行', trigger: 'blur'},
        ],
       },
    }
  },
  methods: {
    onSubmit(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.loading = true;
          this.upload()
        } else {
          this.loading = false;
          return false;
        }
      });
    },
    handleAvatarSuccess(res) {
      this.form.pic = res.msg;
      axios.post('/manage/lobby/addLobby', {
        ...this.form
      }).then(response => {
        this.loading = false;
        let data = response.data;
        // 添加失败
        if (data.code !== 200) {
          this.$message.error(data.msg);
          return;
        }
        // 添加成功
        this.$message({
          message: data.msg,
          type: 'success'
        });
        // 跳转页面
        this.$router.push({
          path: '/lobby/list'
        });
      })
          .catch(error => {
            this.loading = false;
            this.$message.error('系统异常，请联系管理员或稍后再试');
          })
          .finally(() => this.loading = false);
    },
    handleAvatarChangeIcon(file) {
      this.imageUrl = URL.createObjectURL(file.raw);
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === 'image/jpeg';
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error('上传头像图片只能是 JPG 格式!');
      }
      if (!isLt2M) {
        this.$message.error('上传头像图片大小不能超过 2MB!');
      }
      return isJPG && isLt2M;
    },
    upload() {
      // 手动触发文件上传
      this.$refs.upload.submit();
    },
  },
  created() {
    // 获取影厅类型
    getLobbyTypeInfo().then(response => {
      if (response.data.code === 200) {
        this.lobbyTypeOptions = response.data.data
      } else {
        this.$message.error(response.data.msg)
      }
    }).catch(err => console.log(err))
    // 获取影院信息
    getCinemaInfo().then(response => {
      if (response.data.code === 200) {
        this.cinemaOptions = response.data.data
      } else {
        this.$message.error(response.data.msg)
      }
    }).catch(err => console.log(err))
  }
}
</script>

<template>
  <el-form ref="form" v-loading="loading" :rules="rules" :model="form" label-width="80px">
    <el-form-item label="影厅类型">
      <el-select v-model="form.lobbyTypeId" placeholder="请选择品牌">
        <el-option
            v-for="item in lobbyTypeOptions"
            :key="item.id"
            :label="item.lobbyTypeName"
            :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="所属影院">
      <el-select v-model="form.cinemaId" placeholder="请选择品牌">
        <el-option
            v-for="item in cinemaOptions"
            :key="item.id"
            :label="item.cinemaName"
            :value="item.id">
        </el-option>
      </el-select>
    </el-form-item>
    <el-form-item label="影厅名" prop="lobbyName">
      <el-input v-model="form.lobbyName" placeholder="请输入影厅名"></el-input>
    </el-form-item>
    <el-form-item label="影厅描述" prop="introduce">
      <el-input v-model="form.introduce" placeholder="请输入影厅介绍"></el-input>
    </el-form-item>
    <el-form-item label="列数" prop="col">
      <el-input v-model="form.col" placeholder="请输入列"></el-input>
    </el-form-item>
    <el-form-item label="行数" prop="rowNum">
      <el-input v-model="form.rowNum" placeholder="请输入行"></el-input>
    </el-form-item>
    <el-form-item label="影院图片">
      <el-upload style="width: 0;"
                 ref="upload"
                 class="avatar-uploader"
                 action="http://localhost:8081/manage/cinema/uploadPicture"
                 :auto-upload="false"
                 :show-file-list="true"
                 :on-change="handleAvatarChangeIcon"
                 :on-success="handleAvatarSuccess"
                 :before-upload="beforeAvatarUpload">
        <img v-if="this.imageUrl" :src="this.imageUrl" class="avatar">
        <i v-else class="el-icon-plus avatar-uploader-icon"></i>
      </el-upload>
    </el-form-item>
    <el-form-item>
      <el-button type="primary" @click="onSubmit('form')">添加</el-button>
      <el-button>取消</el-button>
    </el-form-item>
  </el-form>
</template>

<style scoped>
.el-select {
  width: 100%;
}

/deep/ .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

/deep/ .avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

/deep/ .avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}


</style>